<?php

namespace app\admin\controller\information;

use app\common\controller\Backend;
use fast\Tree;
use think\Db;
use app\admin\model\Kisapi;

/**
 * 物料档案管理
 *
 * @icon fa fa-circle-o
 */
class Inventoryclass extends Backend
{

    /**
     * Inventory模型对象
     * @var \app\admin\model\information\InventoryClass
     */
    protected $model = null;
    protected $allUpdate =[];
    protected $searchFields = ['cInvCCode','cInvCName'];
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\information\InventoryClass;

    }


    public function synData(){
        //$list = Db::connect('database.sec')->table('InventoryClass')->select();
        $list = Kisapi::getDataByName('inventoryclass');
        $id_arr = [];
        foreach ($list as $k => $v) {
            $data  = [
                'cInvCCode' => $v['fNumber'],
                'cInvCName' => $v['fName'],
                'pid' => $v['fParentId']==0?1:$v['fParentId'],
                'id' => $v['fItemId'],
                'createtime' => date('Y-m-d H:i:s',time())
                //'iInvCGrade' => $v['iInvCGrade'],
                //'bInvCEnd' => $v['bInvCEnd'],
            ];
            
            if(strlen($v['fNumber'])==2){
                $level = 1;
            }else{
                $level = substr_count($v['fNumber'],'.')+1;
            }
            $data['level'] = $level; 
            $id_arr[] = $v['fItemId'];
            $this->model->insert($data,true);
        }
        
        $rows = $this->model::select();
        if($rows){
            foreach($rows as $row){
                if(!in_array($row->id,$id_arr)){
                    $row->delete();
                }
            }
        }

        $this->success("同步完成");
    }

    public function getData(){
        $level = $this->request->get('level');
        $requestArr = $this->request->request();
        
        $this->searchFields = 'cInvCCode,cInvCName';
        $this->selectpageFields = 'id,pid,cInvCCode,cInvCName';
        $requestArr['custom']['level'] = $level;
        $this->request->request($requestArr);
        
        return self::selectpage();
    }
    
    public function getTreeData(){
        //$data = \app\admin\model\information\InventoryClass::geTreeData();

		//setParam(['name'=>'name']);
		$keyword = $this->request->post('cInvCName');
        $where2 = [];
        if($keyword){
            $where2['cInvCName'] = ['like',"%{$keyword}%"];
        }
        
        list($where, $sort, $order, $offset, $limit) = $this->buildparams(null);
        
		$data = $this->model
                ->where($where)
                ->where($where2)
			     ->order($sort,$order)
				->field('id,pid,cInvCName,cInvCCode')
                ->select();
		
        $top = $this->model
                ->where($where)
                ->where($where2)
                ->order([
                    'level'=>'asc',
                    'id'=>'asc'
                ])
                ->field('id,pid,level')
                ->find();
                
                
        $root = 1;
        if($keyword){
           $root = $top->pid;
        }

        $classtree = new Tree();
        $classtree->init($data);
        $treeList = $classtree->getTreeList($classtree->getTreeArray($root), 'cInvCName');
       
       /* foreach ($treeList as $k => $v) {
            $jsList[] = array(
                'id' => $v['id'],
                'parent' => $v['pid'] ? $v['pid'] : '#',
                'text' => __($v['name']),
            );
        }*/
       
        return json(["list" => $treeList,'total'=>count($data)]);

        //$this->success('','',$jsList);//
    }

    public function getTreeData2(){
        $data = \app\admin\model\information\InventoryClass::geTreeData2();
        $classtree = new Tree();
        $classtree->init($data);
        $treeList = $classtree->getTreeList($classtree->getTreeArray(0), 'name');
        
        foreach ($treeList as $k => $v) {
            $jsList[] = array(
                'id' => $v['id'],
                'parent' => $v['pid'] ? $v['pid'] : '#',
                'text' => __($v['name']),
            );
        }
        $this->success('','',$jsList);
    }
}
